# Makefile: Common -------------------------------------------------------------

# Copyright (c) 2010- Peter Bui and Aaron Dingler.
# This software is distributed under the GNU General Public License.
# See the file COPYING for details.

# Commands ---------------------------------------------------------------------

SHELL = /bin/sh

AR  = ar
CC  = gcc
CXX = g++

# Color Commands and Macros ----------------------------------------------------

MAKE_COLOR = $(shell tput setaf 5)
CC_COLOR   = $(shell tput setaf 6)
LINK_COLOR = $(shell tput setaf 2)
END_COLOR  = $(shell tput sgr0)

MAKE_MSG = echo '[$(MAKE_COLOR)MAKE$(END_COLOR)] $(1)'
CC_MSG	 = echo '    [$(CC_COLOR)$(1)$(END_COLOR)] $(2)'
LINK_MSG = echo '    [$(LINK_COLOR)$(1)$(END_COLOR)] $(2)'

# Compiler and Linker Flags ----------------------------------------------------

SMQ_ARFLAGS    = rcs

SMQ_CFLAGS     = -g -Wall -D_GNU_SOURCE $(CFLAGS) -DCCTOOLS_OPSYS_LINUX
SMQ_CFLAGS    += $(INCPATH)
SMQ_CXXFLAGS   = $(SMQ_CFLAGS)

SMQ_LINKFLAGS  = $(LINKFLAGS)
SMQ_LINKFLAGS += $(SMQ_LIBPATH)
SMQ_LINKLIBS   = -lm $(LINKLIBS)

# Include and Library Paths ----------------------------------------------------

SMQ_INCPATH    = -I. $(INCPATH)
SMQ_LIBPATH    = -L. $(LIBPATH)

# File Extension Handlers ------------------------------------------------------

.SUFFIXES: .c .o

.c.o:
	@$(call CC_MSG,CC,$(RELPATH)$@)
	@$(CC) $(SMQ_CFLAGS) -o $@ -c $<

# General Targets --------------------------------------------------------------

all: 	
	@$(call MAKE_MSG,'Building all object and target files')
	@$(MAKE) --no-print-directory $(SMQ_TARGETS)

phony:	clean depend update

clean:
	@$(call MAKE_MSG,'Removing all object and target files')
	@rm -f *.o $(SMQ_TARGETS)

depend:
	@$(call MAKE_MSG,'Generating dependencies automagically')
	@rm -f Makefile.bak
	@mv Makefile Makefile.bak
	@sed '/^# DEPENDENCIES/,$$d' Makefile.bak > Makefile
	@echo '# DEPENDENCIES' >> Makefile
	@echo '' >> Makefile
	@$(CC) $(SMQ_INCPATH) -MM *.c >> Makefile
	@echo '' >> Makefile
	@echo '# vim: sts=4 sw=4 ts=8 ft=make' >> Makefile

update: clean depend all

# vim: sts=4 sw=4 ts=8 ft=make
